package com.swxc.smartorder.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.swxc.smartorder.R;

/**
 * Author   Administrator
 * Date     2018/4/9
 * Desc     eg
 *
 * @author Administrator
 */
public class ToastUtil {

    public final static int STATUS_SUCCESS = 0;
    public final static int STATUS_COMMON = 1;
    public final static int STATUS_ERROR = 2;
    private LinearLayout mLayout;
    private Toast mToast;
    private TextView mTextView;
    private ImageView mImageView;
    private TimeCount timeCount;
    private String message;
    private Handler mHandler = new Handler();
    private boolean canceled = true;

    public ToastUtil(Context context, boolean isSuccess, int status, String msg) {
        message = msg;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //自定义布局
        View view = inflater.inflate(R.layout.layout_prompt, null);

        int screenWidth = ScreenUtil.getScreenWidth(context);
        int margin = DisplayUtil.INSTANCE.dip2px(context, 10);
        int height = DisplayUtil.INSTANCE.dip2px(context, 50);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth - 2 * margin, height);
        mLayout = view.findViewById(R.id.layout_prompt);
        mLayout.setLayoutParams(layoutParams);
        mTextView = view.findViewById(R.id.tv_prompt);
        mImageView = view.findViewById(R.id.iv_prompt);
        mTextView.setText(msg);
        Log.i("ToastUtil", "Toast start...");
        if (mToast == null) {
            mToast = new Toast(context);
            Log.i("ToastUtil", "Toast create...");
        }
        if (isSuccess) {

            mImageView.setImageResource(R.mipmap.icon_success);
        } else {
            mImageView.setImageResource(R.mipmap.icon_default);

        }
        switch (status) {
            case STATUS_COMMON:
                mLayout.setBackgroundResource(R.drawable.shape_corner_common);
                break;
            case STATUS_SUCCESS:
                mLayout.setBackgroundResource(R.drawable.shape_corner_green);
                break;
            case STATUS_ERROR:
                mLayout.setBackgroundResource(R.drawable.shape_corner_red);
                break;
            default:
                mLayout.setBackgroundResource(R.drawable.shape_corner_green);
                break;
        }
        mToast.setGravity(Gravity.BOTTOM, 0, margin);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(view);
    }

    /**
     * 自定义居中显示toast
     */
    public void show() {
        mToast.show();
        Log.i("ToastUtil", "Toast show...");
    }

    /**
     * 自定义时长、居中显示toast
     *
     * @param duration
     */
    public void show(int duration) {
        timeCount = new TimeCount(duration, 1000);
        Log.i("ToastUtil", "Toast show...");
        if (canceled) {
            timeCount.start();
            canceled = false;
            showUntilCancel();
        }
    }

    /**
     * 隐藏toast
     */
    private void hide() {
        if (mToast != null) {
            mToast.cancel();
        }
        canceled = true;
        Log.i("ToastUtil", "Toast that customed duration hide...");
    }

    private void showUntilCancel() {
        if (canceled) {
            //如果已经取消显示，就直接return
            return;
        }
        mToast.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("ToastUtil", "Toast showUntilCancel...");
                showUntilCancel();
            }
        }, Toast.LENGTH_LONG);
    }

    /**
     * 自定义计时器
     */
    private class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTextView.setText(message);
        }

        @Override
        public void onFinish() {
            hide();
        }
    }
}
